//
//  MainRootViewController.m
//  MirrorCharge
//
//  Created by dllo on 16/10/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MainRootViewController.h"
#import "BaseCollectionView.h"
#import "NormalCollectionViewDataSource.h"
#import "MainNavigationTitleView.h"
#import "BaseTabBar.h"
#import "HLMainModel.h"
#import "HLMacro.h"
#import "LocalJSONAnalytical.h"
#import "YYModel.h"
#import "MainAddBook.h"

// cell
#import "MainDailyBookCollectionCell.h"
#import "MainSummaryCollectionCell.h"
#import "MainAddBookCollectionCell.h"

@interface MainRootViewController ()<UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet BaseCollectionView *collectionView;
@property (weak, nonatomic) IBOutlet BaseTabBar *tabBar;

@property (weak, nonatomic) IBOutlet MainNavigationTitleView *titleView;

@property (nonatomic, strong) NormalCollectionViewDataSource *dataSource;

@property (nonatomic, strong) HLMainModel *mainModel;

@end

@implementation MainRootViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self setupCollectionView];
}

- (void)handleData {
    NSDictionary *result = [LocalJSONAnalytical JSONAnalyticalWithFileName:@"MainModel"];
    _mainModel = [HLMainModel yy_modelWithDictionary:result];
}

- (void)setupCollectionView {
    NSArray *reuses = @[@"MainDailyBookCollectionCell", @"MainSummaryCollectionCell", @"MainAddBookCollectionCell"];
    for (NSString *reuse in reuses) {
        [_collectionView registerNib:[UINib nibWithNibName:reuse bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:reuse];
    }
    _dataSource = [NormalCollectionViewDataSource NormalCollectionViewDataSource];
    _dataSource.sourceArray = @[_mainModel.summary, _mainModel.dailyBook, [MainAddBook new]];
    _collectionView.delegate = self;
    _collectionView.dataSource = _dataSource;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        self.navigationItem.title = @"汇总";
    } else if (indexPath.item == 1) {
        self.navigationItem.title = @"明镜日常账本";
    } else {
        self.navigationItem.title = @"添加新账本";
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return collectionView.frame.size;
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
